package npr;


import processing.core.*;

import javax.media.opengl.*;
import com.sun.opengl.util.*;
import java.util.LinkedList;
import java.util.ListIterator;
import javax.media.opengl.*;
import com.sun.opengl.util.*;


public class ApparentRidges extends ObjectSpaceLines{


    /**
     * Specifies whether lines should be drawn faded.
     */
    protected boolean drawFaded= true;
    

   /** 
     * Creates an ApparentRidges object.
     */
    public ApparentRidges(){
    }

    /**
     * Specifies whether lines should be drawn faded.
     *
     * @param drawFaded true if lines are to be faded, false if not.
     * The default is true.
     */
    public void drawFaded(boolean drawFaded){
	
	this.drawFaded= drawFaded;
    }


    public void postProcess(){
	
	renderer= (NPR)parent.g;
	gl= renderer.gl;

	gl.glEnable(GL.GL_LINE_SMOOTH);

	OBJModel model;
	for(int i=start; i<stop; i++){
	    float modelMatrix[]= renderer.getModel(i);
	    float modelviewMatrix[]= renderer.getModelview(i);

	    PVector eye= renderer.getEye(i);

	    PMatrix3D modelviewMatrixInv= new PMatrix3D(modelviewMatrix[0],
					      modelviewMatrix[4],
					      modelviewMatrix[8],
					      modelviewMatrix[12],
					      modelviewMatrix[1],
					      modelviewMatrix[5],
					      modelviewMatrix[9],
					      modelviewMatrix[13],
					      modelviewMatrix[2],
					      modelviewMatrix[6],
					      modelviewMatrix[10],
					      modelviewMatrix[14],
					      modelviewMatrix[3],
					      modelviewMatrix[7],
					      modelviewMatrix[11],
					      modelviewMatrix[15]);
	    modelviewMatrixInv.invert();
	    eye= modelviewMatrixInv.mult(eye, null);
	  
	    
	    gl.glMatrixMode(GL.GL_MODELVIEW);
	    gl.glPushMatrix();
	    gl.glTranslatef(0, 0, width);
	    gl.glMultMatrixf(modelviewMatrix, 0);

	    ListIterator<OBJModel> listIterator= renderer.getModels(i).listIterator();
	    while(listIterator.hasNext()){    
		model= listIterator.next();
		if(drawHiddenLines){
		    gl.glDisable(GL.GL_DEPTH_TEST);
		    gl.glLineWidth(hiddenWidth);
		    model.drawApparentRidges(eye,
					     true, drawFaded, threshold,
					     hiddenRed, hiddenGreen, hiddenBlue, hiddenAlpha);
		    
		    gl.glEnable(GL.GL_DEPTH_TEST);
		}
		gl.glDepthMask(false);
		gl.glLineWidth(width);
		model.drawApparentRidges(eye,
					 true, drawFaded, threshold,
					 red, green, blue, alpha);
		gl.glDepthMask(true);
	    }
	    gl.glPopMatrix();
	}
	gl.glDisable(GL.GL_LINE_SMOOTH);
    }
}